package com.hxkj.service.article.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.ArticleCollectionServiceClient;
import com.hxkj.domain.po.UserArticle;
import com.hxkj.service.article.ArticleCollectionService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * created by chenfangzhou on 2018/8/13
 */
@Service
@Slf4j
public class ArticleCollectionServiceImpl implements ArticleCollectionService {

    @Autowired
    private ArticleCollectionServiceClient articleCollectionServiceClient;

    // 收藏文章
    @Override
    public ResultMessageStruct collectArticle(UserArticle userArticle) throws Exception {
        try {
            ResultMessageStruct result;
            if (StringUtils.isBlank(userArticle.getTitle())) throw new CustomMessageException(401, "文章标题为空!");
            if (StringUtils.isBlank(userArticle.getUrl())) throw new CustomMessageException(401, "文章url地址为空!");
            if (userArticle.getSiteId() == null) throw new CustomMessageException(401, "站点id为空!");
            result = articleCollectionServiceClient.collectArticle(userArticle);
            if (result.getCode() == 301) throw new CustomMessageException(401, "文章已收藏,请勿重复收藏文章!");
            if (result.getCode() != 200) throw new CustomMessageException(401, "文章收藏失败!");
            return result;
        } catch (CustomMessageException e) {
            e.printStackTrace();
            throw e;
        }
    }

    // 取消收藏
    @Override
    public ResultMessageStruct cancelCollectArticle(UserArticle userArticle) throws Exception {
        try {
            ResultMessageStruct result = new ResultMessageStruct();
            //参数校验
            if (StringUtils.isBlank(userArticle.getTitle())) throw new CustomMessageException(401, "文章标题为空!");
            if (userArticle.getSiteId() == null) throw new CustomMessageException(401, "站点id为空!");
            result = articleCollectionServiceClient.cancelCollectArticle(userArticle);
            if (result.getCode() != 200) throw new CustomMessageException(401, "文章取消收藏失败!");
            return result;
        } catch (CustomMessageException e) {
            e.printStackTrace();
            throw e;
        }
    }

    // 查看收藏列表
    @Override
    public PageInfo<UserArticle> getCollectionList(UserArticle userArticle, PageTo pageParam) throws Exception {
        try {
            if (userArticle.getSiteId() == null) throw new CustomMessageException(401, "站点id为空!");
            PageInfo<UserArticle> result;
            ParameterDataStruct<UserArticle> parameterDataStruct = new ParameterDataStruct<>();
            parameterDataStruct.setData(userArticle);
            if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
                pageParam.setPageSize(10);
                pageParam.setPageNum(1);
            }
            parameterDataStruct.setPageTo(pageParam);
            result = articleCollectionServiceClient.getCollectionList(parameterDataStruct);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}
